<?php
/**
 * Created by a front-end programmer.
 * User: Xwen
 * Date: 2018/2/27
 * Time: 10:46
 * Mail:lauwencn@gmail.com
 */
class ArticleModel extends Model{
    public $table = 'article';
    public $error = '请再次尝试……';
    public function __construct($table = NULL)
    {
        $table = $this->table;
        parent::__construct($table);
    }
    public function get_all(){
        return $this->all();
    }
    public function get_one($aid){
        return $this->where('aid='.$aid)->find();
    }
    public function edit($aid){
        if(!$this->validate()) return false;
        return $this->where('aid='.$aid)->update();
    }
    public function remove($aid){
        $this->where("aid=".$aid)->delete();
    }
    public function add_data(){
        if(!$this->validate()) return false;
        $_POST['sendtime'] = time();
        return $this->add();
    }
    public function validate(){
        if(empty($_POST['title'])){
            $this->error = '文章标题不能为空';
            return false;
        }
        if(empty($_POST['category_cid'])){
            $this->error = '请选择文章类型';
            return false;
        }
        if(empty($_POST['content'])){
            $this->error = '文章内容不能为空';
            return false;
        }
        if (!empty($_FILES["thumb"]["name"])){
            $file = $_FILES['thumb'];
            $upload = new UploadFile(true, './Upload/', array('jpg', 'jpeg', 'png'));
            $path = $upload->upload_file($file);
            if($path){
                $_POST['thumb'] = $path;
            }else{
                $this->error = $upload->get_msg();
                return false;
            }
        }
        return true;
    }
    public function inc($aid){
        return $this->exe('update '.$this->table.' set click=click+1 where aid='.$aid);
    }

}

















?>